package multidraw;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import multidraw.model.DrawingCanvasModel;
import multidraw.model.ModelObserver;
import multidraw.model.Shape;
import multidraw.tools.SelectorTool;



public class ControlPanelView extends JPanel implements ModelObserver {
	private DrawingCanvasModel myModel;
	private ControlPanelController controller;
	private MultiDraw1 client;
	
	protected JPanel drawingOptions;
	protected JButton clearButton;
	protected JLabel colorLabel;
	protected JComboBox colorBox;
	protected JButton fillButton;
	protected JButton connectionsButton;
	
	protected JPanel connectionOptions;
	protected JButton loginButton;
	protected JButton createButton;
	protected JButton connectButton;
	protected JButton drawingButton;
	
	public ControlPanelView(DrawingCanvasModel model) {
		this.myModel = model;
		
		this.setLayout(new BorderLayout());
		
		drawingOptions = new JPanel();
		drawingOptions.setLayout(new FlowLayout());
		
		clearButton = new JButton("Clear");
	    drawingOptions.add(clearButton);
	    
	    colorLabel = new JLabel("Pen color");
	    drawingOptions.add(colorLabel);
	    colorBox = new JComboBox();
	    colorBox.addItem("black");
	    colorBox.addItem("blue");
	    colorBox.addItem("green");
	    colorBox.addItem("red");
	    drawingOptions.add(colorBox);
	    
	    fillButton = new JButton("(Un)fill");
	    drawingOptions.add(fillButton);
	    
	    connectionsButton = new JButton("Connection Options");
	    drawingOptions.add(connectionsButton);
	    
	    add(drawingOptions, BorderLayout.NORTH);
	    
	    connectionOptions = new JPanel();
	    connectionOptions.setLayout(new FlowLayout());
	    
	    loginButton = new JButton("Log in...");
	    connectionOptions.add(loginButton);
	    
	    createButton = new JButton("Create Session");
	    createButton.setEnabled(false);
	    connectionOptions.add(createButton);
	    
	    connectButton = new JButton("Connect...");
	    connectButton.setEnabled(false);
	    connectionOptions.add(connectButton);
	    
	    drawingButton = new JButton("Drawing Options");
	    connectionOptions.add(drawingButton);
	    
	    add(connectionOptions, BorderLayout.SOUTH);
	    connectionOptions.setVisible(false);
	    
	    myModel.attachObserver(this);
	    
	    addControlPanelListener(new ControlPanelController(myModel, this));
	}
	
	protected void addControlPanelListener(ControlPanelController controller) {
		clearButton.addActionListener((ActionListener)controller);
	    colorBox.addItemListener((ItemListener)controller);
	    fillButton.addActionListener((ActionListener)controller);
	    drawingButton.addActionListener((ActionListener) controller);
	    connectionsButton.addActionListener((ActionListener) controller);
	    loginButton.addActionListener((ActionListener) controller);
	    createButton.addActionListener((ActionListener) controller);
	    connectButton.addActionListener((ActionListener) controller);
	}
	
	public void updateContent() {
		//reflect the selected tool if any.
		if (myModel.getCurrentTool() != null) {
			if (myModel.getCurrentTool() instanceof SelectorTool) {
				//change the combo box to the value of selected Object.
				//then since now the if you change the combo box value, then you change the object.
				Shape selectedObject = myModel.getSelectedObject();
				if (selectedObject != null)
					colorBox.setSelectedItem(
							colorToItem(selectedObject
									.getPenColor()));
				else 
					colorBox.setSelectedItem(
							colorToItem(myModel.getPenColor()));
			} else {
				colorBox.setSelectedItem(
						colorToItem(myModel.getPenColor()));
			}
		}
	}
	
	protected Object colorToItem(Color c) {
	    if( c == Color.black ) {
	      return "black";
	    }
	    
	    if( c == Color.blue) {
	      return "blue";
	    }
	    else if(c == Color.red) {
	      return "red";
	    }
	    else if(c == Color.green) {
	      return "green";
	    }
	    else {
	      return null;
	    }
	  }
	
	public JButton getFillButton() {
		return fillButton; 
	}
	
	public JButton getClearButton() {
		return clearButton;
	}
	
	public JButton getDrawingButton() {
		return drawingButton;
	}
	
	public JButton getConnectionsButton() {
		return connectionsButton;
	}
	
	public JButton getLoginButton() {
		return loginButton;
	}
	
	public JButton getCreateButton() {
		return createButton;
	}
	
	public JButton getConnectButton() {
		return connectButton;
	}
	
	public void switchToDrawing() {
		connectionOptions.setVisible(false);
		drawingOptions.setVisible(true);
	}
	
	public void switchToConnection() {
		drawingOptions.setVisible(false);
		connectionOptions.setVisible(true);
	}
	
	public void setClient(MultiDraw1 c) {
		client = c;
	}
	
	public ClientListPanelView openClientList(String name) {
		return client.openClientList(name);
	}
	
}
